package com.zik.lredisdemo.controller;

import com.zik.lredisdemo.entity.User;
import com.zik.lredisdemo.service.IUserService;
import com.zik.lredisdemo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 初始3rd_session有效期 单位s  有效期为一天
     */
    private static final int expire = 60 * 60 * 24;
//    private static final int expire = 5;

    @Autowired
    private RedisUtil redisUtil;


    @Resource(name = "IUserService")
    private IUserService userService;


    /**
     * 获取用户策略：先从缓存中获取用户，没有则取数据表中的数据，再将数据写入缓存
     *
     * @param userId
     * @return
     */
    @GetMapping("/getData/{userId}")
    public Map<String, String> getUserInfoById(@PathVariable String userId) {

        String userKey = "user_" + userId;

        //从redis存储中取所需数据
        Map<String, String> redisDataMap = redisUtil.hgetAll(userKey);

        if (redisDataMap == null || redisDataMap.size() == 0) {
            System.out.println("缓存不存在");
            //缓存不存在
            //-从数据库中获取用户信息
            User userInfo = userService.getById(userId);
            //-把用户信息存入redis中
            redisDataMap.put("user_id", String.valueOf(userInfo.getId()));
            redisDataMap.put("user_username", String.valueOf(userInfo.getUsername()));
            redisDataMap.put("user_password", String.valueOf(userInfo.getPassword()));
            //插入缓存
            redisUtil.hmset(userKey, redisDataMap, expire);
        } else {
            System.out.println("缓存存在,用户信息：" + redisDataMap.toString());
        }
        return redisDataMap;
    }
    @GetMapping("/getData2/{userId}")
    public Map<String,String> getUserById2(@PathVariable String userId){
        String userKey = "user_" + userId;

        //从redis存储中取所需数据
        Map<String, String> redisDataMap = redisUtil.hgetAll(userKey);

        if (redisDataMap == null || redisDataMap.size() == 0) {
            log.info("缓存不存在");

            //-从数据库中获取用户信息
            User userInfo = userService.getById(userId);
            if (userInfo == null){
                log.info("缓存空数据 --> ");
                redisDataMap.put("flag","-1");
                redisUtil.hmset(userKey,redisDataMap,expire);
            }else {
                //-把用户信息存入redis中
                redisDataMap.put("flag","1");
                redisDataMap.put("user_id", String.valueOf(userInfo.getId()));
                redisDataMap.put("user_username", String.valueOf(userInfo.getUsername()));
                redisDataMap.put("user_password", String.valueOf(userInfo.getPassword()));
                //插入缓存
                redisUtil.hmset(userKey, redisDataMap, expire);
            }
        }else {
            if(redisDataMap.get("flag").equals("-1")){
                log.info("空数据判定生效");
                return null;
            }
            System.out.println("缓存存在,用户信息：" + redisDataMap.toString());
        }
        return redisDataMap;

    }

}
